<template>
  <div class="login f-c-c">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="54px"
      class="bounce-in"
    >
      <h1 class="f-c-c">HXOV 后台管理中心</h1>
      <el-form-item label="账号" prop="pass">
        <el-input
          v-model="ruleForm.pass"
          placeholder="请输入账号/手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="age">
        <el-input style="width: 145px" placeholder="请输入验证码" v-model.number="ruleForm.age"></el-input>
        <img id="code-img" src="http://www.hxov.com:7001/public/getCaptcha?type=data"/>
      </el-form-item>
      <el-form-item>
        <el-button id="submit" type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "login",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  height: 100vh;
  width: 100vw;
  background: url("../assets/img/loginBg.jpg") 0 0/100% 100%;
}
h1{
    color: #fff;
    padding: 30px 0;
    font-weight: normal;
    margin-bottom: 50px;
    background: $theme-color;
}
.el-form {
  height: 500px;
  width: 500px;
  box-sizing: border-box;
  border-radius: $border-radius-big;
  background: rgba(255, 255, 255, 0.5);
  overflow: hidden;
}
.el-form-item{
    padding: 0 80px;
    margin-bottom: 30px;
}
#code-img {
  height: 40px;
  width: 100px;
  vertical-align: bottom;
  margin-left: 15px;
  border-radius: $border-radius-base;
  background: #ddd;
}
#submit{
    width: 100%;
}
</style>